package org.hnxxxy.ebms.config;

import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.hnxxxy.ebms.filter.JwtFilter;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Description: Shiro 设置
 * @Author WANGWEI
 * @Date: 2021/1/20  20:13
 * @EMAIL: 1377366738@qq.com
 */
@Configuration
public class ShiroConfig {


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);

        // 添加 jwt 过滤器，拦截除login外的请求
        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("jwt",jwtFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        LinkedHashMap<String,String> filterRuleMap = new LinkedHashMap<>();
        // 所有请求都通过 jwtfilter
        filterRuleMap.put("/user/login","anon");
        filterRuleMap.put("/user/register","anon");
        filterRuleMap.put("/doc.html","anon");
        filterRuleMap.put("/**","jwt");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterRuleMap);
        return shiroFilterFactoryBean;
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultSecurityManager(@Qualifier("userRealm") UserRealm userRealm){
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        defaultSecurityManager.setRealm(userRealm);
        return defaultSecurityManager;
    }

    /**
     * @Description: Jwt 拦截器
     * @Author WANGWEI
     * @Date: 2021/1/20  20:27
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    public JwtFilter jwtFilter(){
        return new JwtFilter();
    }

    /**
     * @Description: 为Spring-Bean 开启Shiro的注解支持
     * @Author WANGWEI
     * @Date: 2021/1/20  20:18
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    /**
     * @Description:
     * @Author WANGWEI
     * @Date: 2021/1/20  20:19
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        return new DefaultAdvisorAutoProxyCreator();
    }
    /**
     * @Description: 交给spring来管理Shiro Bean的生命周期
     * @Author WANGWEI
     * @Date: 2021/1/20  20:16
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean
    public static LifecycleBeanPostProcessor getLifecycleBeanPostProcessor(){
        return new LifecycleBeanPostProcessor();
    }


    /**
     * @Description: 确定Realm
     * @Author WANGWEI
     * @Date: 2021/1/20  20:14
     * @EMAIL: 1377366738@qq.com
     **/
    @Bean("userRealm")
    public UserRealm getUserRealm(){
        return new UserRealm();
    }

}
